//
//  TextInputView.m
//  RainbowFM
//
//  Created by Kratos on 14/12/14.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "TextInputView.h"
#import "PlayAudioManager.h"

#define DESTION_IMG_SIZE    500*1024

@implementation TextInputView
-(void)awakeFromNib
{
    _manage = [[VoiceManage alloc]init];
    _manage.voiceRecorderDelegate = self;
    [_manage addlongPressGestureWith:_sendVoiceBtn];
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if ([text isEqualToString:@"\n"]){
        [_contentTextView resignFirstResponder];
        return NO;
    }
    
    return YES;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if([self.delegate respondsToSelector:@selector(textSended:view:)]){
        [self.delegate textSended:_contentTextView.text view:self];
    }
    
    _contentTextView.text = @"";
    [_contentTextView resignFirstResponder];
    
    return YES;
}

-(IBAction)sendImage:(id)sender
{
   
    [_contentTextView resignFirstResponder];
    if(_delegate && [_delegate isKindOfClass:[UIViewController class]]){
        BOOL canUseCamera = [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera];
        
        UIActionSheet *actionSheet = nil;
        if (canUseCamera) {
            actionSheet  = [[UIActionSheet alloc]initWithTitle:nil
                                                      delegate:self
                                             cancelButtonTitle:@"取消"
                                        destructiveButtonTitle:nil
                                             otherButtonTitles:@"拍照",@"从相册选取", nil];
        }
        else
        {
            actionSheet = [[UIActionSheet alloc]initWithTitle:nil
                                                     delegate:self
                                            cancelButtonTitle:@"取消"
                                       destructiveButtonTitle:nil
                                            otherButtonTitles:@"从相册选取", nil];
        }
        [actionSheet showInView:[(UIViewController *)_delegate view]];
    }

}

-(IBAction)exchangeVoice:(id)sender
{
    [_contentTextView resignFirstResponder];
    [self exchangeMode:1];
}

-(IBAction)exchangeText:(id)sender
{
    [_contentTextView becomeFirstResponder];
    [self exchangeMode:0];
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    BOOL canUseCamera = [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera];
    
    UIImagePickerController *picker = nil;
    if (canUseCamera) {
        switch (buttonIndex) {
            case 0:
            {
                picker = [[UIImagePickerController alloc] init];
                [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
            }
                break;
            case 1:
            {
                picker = [[UIImagePickerController alloc] init];
                [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            }
                break;
            default:
                break;
        }
    }
    else
    {
        if (buttonIndex == 0) {
            picker = [[UIImagePickerController alloc] init];
            [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        }
    }
    
    if (picker) {
        picker.delegate = self;
        picker.videoQuality = UIImagePickerControllerQualityTypeLow;
        [(UIViewController *)_delegate presentViewController:picker animated:YES completion:^{
        }];
    }
    
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *editedImage = info[UIImagePickerControllerOriginalImage];
    //editedImage = [editedImage scaleImageWithSize:0.2];
    
    
    if (editedImage) {
        NSData *contentData = UIImageJPEGRepresentation(editedImage, 1.0);
        //图片大了，进行压缩
        if(contentData.length>DESTION_IMG_SIZE)
        {
            float scale = (float)DESTION_IMG_SIZE/contentData.length;
            NSLog(@"压缩比例为:%.04f",scale);
            contentData =UIImageJPEGRepresentation(editedImage, scale);
        }
        
        if (contentData) {

            if ([_delegate respondsToSelector:@selector(imageSendedWithImage:imageData:view:)]) {
                [_delegate imageSendedWithImage:editedImage imageData:contentData view:self];
            }
        }
        
    }
    
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}


#pragma mark - VoiceManageDelegate
-(void)didFinishRecord:(VoiceManage*)manage encodeVoiceData:(NSData*)ecodeData
{
    if([self.delegate respondsToSelector:@selector(voiceSendedWithVoiceData:view:)]){
        [self.delegate voiceSendedWithVoiceData:ecodeData view:self];
    }
    [_contentTextView resignFirstResponder];
}

-(void)voiceRecorder:(VoiceManage*)manage errorWithState:(NSUInteger)state
{
    
}

-(void)exchangeMode:(NSInteger)mode
{
    _mode = mode;
    if (_mode == 0) {
        [_contentTextView setHidden:NO];
        [_textBG setHidden:NO];
        [_changeVoiceBtn setHidden:NO];
        [_sendImageBtn setHidden:NO];
        
        [_changeTxtBtn setHidden:YES];
        [_sendVoiceBtn setHidden:YES];
    }else{
        [_contentTextView setHidden:YES];
        [_textBG setHidden:YES];
        [_changeVoiceBtn setHidden:YES];
        
        [_changeTxtBtn setHidden:NO];
        [_sendImageBtn setHidden:NO];
        [_sendVoiceBtn setHidden:NO];
    }
}


@end
